﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

//1 从配置文件里面加载数据
//2 根据数据创建一个具体的Item （根据道具的类型加载）
//3 点击具体的Item 弹出详细信息界面
//4 实现动画效果（背包界面的左右切入）
//5 道具类型切换功能

public class BackpackPanel : MonoBehaviour
{
    //模板
    public RectTransform ItemTemp;
    public ScrollRect ItemScrollRect;
    public BackpackItemDetail LeftBackpackItemDetail;
    public Button SellButton;

    public Transform LeftOBj;
    public Transform LeftFrom;
    public Transform LeftTo;

    public Transform RightOBj;
    public Transform RightFrom;
    public Transform RightTo;

    private Tweener LeftTween;
    private Tweener RightTween;

    private List<GameObject> ItemOBJList;

	// Use this for initialization
	void Awake ()
    {
        NewItemManager.Instance.LoadItemConfigData();
        this.LeftOBj.gameObject.SetActive(false);
        this.RightOBj.gameObject.SetActive(false);
        this.LeftOBj.localPosition = this.LeftFrom.localPosition;
        this.RightOBj.localPosition = this.RightFrom.localPosition;
        this.SellButton.onClick.AddListener(() =>
        {
            // 1 详细界面做动画 做返回的动画
            this.LeftTween.PlayBackwards();
        });
    }
    private void OnEnable()
    {
        this.CreateItemsByConfig(BackpackItem.ItemType.UnKown);
    }

    private void Start()
    {
        this.LeftTween = this.LeftOBj.DOLocalMoveX(this.LeftTo.localPosition.x,0.5f).OnStart(()=>{
            this.LeftOBj.gameObject.SetActive(true);
        });
        this.RightTween = this.RightOBj.DOLocalMoveX(this.RightTo.localPosition.x, 0.5f).OnStart(() =>
        {
            this.RightOBj.gameObject.SetActive(true);
        });

        this.LeftTween.SetAutoKill(false);
        this.LeftTween.Pause();
        this.RightTween.SetAutoKill(false);

    }



    private void CreateItemsByConfig(BackpackItem.ItemType itemtype)
    {
        var list = NewItemManager.Instance.BackPackItemList;
        if ( list == null) return;
        if (this.ItemOBJList == null) this.ItemOBJList = new List<GameObject>();

        int index = 0;
        for (int i = 0; i < list.Count; i++)
        {
            if (itemtype != list[i].MItemType) continue;

            index++;
            GameObject go = null;
            if (index  > this.ItemOBJList.Count)
            {
                go = this.CreateItemByTemp();
                this.ItemOBJList.Add(go);
            }
            else
            {
                go = this.ItemOBJList[index - 1];
            }

           
            go.SetActive(true);
            BackpackItemInfo info = go.GetComponent<BackpackItemInfo>();
            if (info == null) 
            {
                go.SetActive(false);
            }

            info.SetData(list[i]);
        }

        if (index < this.ItemOBJList.Count)
        {
            for (int i = index -1;i<this.ItemOBJList.Count;i++)
            {
                this.ItemOBJList[index].SetActive(false);
            }
        }
        
    }

    private GameObject CreateItemByTemp()
    {
        GameObject item = GameObject.Instantiate<GameObject>(this.ItemTemp.gameObject, this.ItemScrollRect.content);
        return item;
    }

    public void AllItemBtnClicked(Toggle t)
    {
        if (!t.isOn) return;
        this.CreateItemsByConfig(BackpackItem.ItemType.UnKown);
    }
    public void EquipItemBtnClicked(Toggle t)
    {
        if (!t.isOn) return;
        this.CreateItemsByConfig(BackpackItem.ItemType.Equip);
    }
    public void ChipItemBtnClicked(Toggle t)
    {
        if (!t.isOn) return;
        this.CreateItemsByConfig(BackpackItem.ItemType.Chips);
    }

    public void ItemClicked(BackpackItemInfo info)
    {
        this.LeftBackpackItemDetail.SetData(info.itemInfo);
        this.LeftTween.PlayForward();
    }

    public void ClosePanel()
    {
        this.LeftTween.PlayBackwards();
        this.RightTween.PlayBackwards();
        GameObject.Destroy(this.gameObject, 0.5f);
    }
}
